package com.syl.practice.nullValue.optional;

import com.syl.practice.nullValue.optional.pojo.Address;
import com.syl.practice.nullValue.optional.pojo.Country;
import com.syl.practice.nullValue.optional.pojo.User;

import java.util.Optional;

public class OptionalTest {

    public static void main(String[] args) {
        User user1 = new User("tom", new Address(new Country("耶路撒冷")));
        User user2 = new User("tom", new Address(null));
        demo01(user1);
        demo01(user2);
        demo02();
    }

    /**
     * Optional简单使用 , 替代if else
     *
     * @param user
     */
    public static void demo01(User user) {
        String ioscode = Optional.ofNullable(user)
                .flatMap(User::getAddress)
                .flatMap(Address::getCountry)
                .map(Country::getIsocode)
                .orElse("default");
        System.out.println(ioscode);
    }

    public static void demo02() {
        User user = new User("hehe", null);
        Optional<Address> address = user.getAddress();
        System.out.println(address.isPresent());
    }

}
